package com.cainiao.service.deptserviceimpl;

import com.cainiao.mapper.EmpExprMapper;
import com.cainiao.mapper.EmpMapper;
import com.cainiao.pojo.*;
import com.cainiao.service.EmpLogService;
import com.cainiao.service.EmpService;
import com.cainiao.utils.JwtUtils;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.time.LocalDateTime;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class EmpServiceImpl implements EmpService {
    //注入
    @Autowired
    private EmpMapper empMapper;
    @Autowired
    private EmpExprMapper empExprMapper;

    @Autowired
    private EmpLogService empLogService;


    @Override
    public PageBean page(EmpQueryParam empQueryParam) {
        PageHelper.startPage(empQueryParam.getPage(),empQueryParam.getPageSize());
        List<Emp> emplist = empMapper.page(empQueryParam);
        Page<Emp> page=(Page<Emp>) emplist;
        return new PageBean(page.getTotal(),page.getResult());
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void add(Emp emp) {
        try {
            emp.setCreateTime(LocalDateTime.now());
            emp.setUpdateTime(LocalDateTime.now());
            empMapper.add(emp);
            List<EmpExpr> exprList = emp.getExprList();
            if (exprList != null && exprList.size()>0){
                exprList.forEach(expr -> expr.setEmpId(emp.getId()));
                empExprMapper.add(exprList);
            }
        } finally {
            EmpLog eng = new EmpLog();
            eng.setOperateTime(LocalDateTime.now());
            eng.setInfo("新增员工信息："+emp);
            empLogService.insertLog(eng);
        }
    }

    @Override
    public List<Emp> findEmps() {
        return empMapper.findEmps();
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void delete(List<Integer> ids) {
        //删除员工
        empMapper.deleteByIds(ids);
        //删除员工对应的expr
        empExprMapper.deleteByEmpIds(ids);
    }

    @Override
    public Emp findById(Integer id) {
        return empMapper.findById(id);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void update(Emp emp) {
        //更新员工信息
        emp.setUpdateTime(LocalDateTime.now());
        empMapper.update(emp);
        //更新expr
        empExprMapper.deleteByEmpIds(Arrays.asList(emp.getId()));
        //添加expr
        List<EmpExpr> exprList = emp.getExprList();
        if (exprList != null && exprList.size()>0){
            exprList.forEach(expr -> expr.setEmpId(emp.getId()));
            empExprMapper.add(exprList);
        }
    }

    @Override
    public LoginInfo login(Emp emp) {
        Emp empResult=empMapper.findByUsernameAndPassword(emp);
        if(empResult!=null){
            Map<String,Object> claims=new HashMap<>();
            claims.put("id",empResult.getId());
            claims.put("username",empResult.getUsername());
            String token = JwtUtils.generateJwt(claims);
            return new LoginInfo(empResult.getId(),empResult.getUsername(),empResult.getName(),token);
        }
        return null;
    }

    @Override
    public JobCountDate empJobData() {
        List<Map<String, Object>> maps = empMapper.empJobData();
        List<Object> jobList = maps.stream().map(map -> map.get("jobList")).toList();
        List<Object> countList = maps.stream().map(map -> map.get("count")).toList();
        return new JobCountDate(jobList,countList);
    }

    @Override
    public List empGenderData() {
        return empMapper.empGenderData();
    }

}
